﻿using System;
using PivotMaker.WinFormApplication.Controls;
using System.Collections.ObjectModel;
using PivotCollectionTools.Data;

namespace PivotMakerApplicationTdd
{
    public class MockCategoryListView : ICategoryListView
    {
        private CategoryListPresenter presenter;

        public MockCategoryListView(PivotTable table)
        {
            this.SelectedIndex = 0;
            this.presenter = new CategoryListPresenter(this, table);
            this.categoyList = new Collection<string>();
        }

        public MockCategoryListView()
            : this(new PivotTable())
        {
        }

        public event EventHandler AddNewCategoryClicked;
        
        public event EventHandler DeleteCategoryClicked;

        public event EventHandler AddNewColumnClicked;

        public event EventHandler DeleteColumnClicked;

        public event EventHandler CategoryItemSelected;

        private void OnAddNewCategoryClicked()
        {
            if (this.AddNewCategoryClicked != null)
            {
                this.AddNewCategoryClicked(this, new EventArgs());
            }
        }

        private void OnDeleteCategoryClicked()
        {
            if (this.DeleteCategoryClicked != null)
            {
                this.DeleteCategoryClicked(this, new EventArgs());
            }
        }

        private void OnAddNewColumnClicked()
        {
            if (this.AddNewColumnClicked != null)
            {
                this.AddNewColumnClicked(this, new EventArgs());
            }
        }

        private void OnDeleteColumnClicked()
        {
            if (this.DeleteColumnClicked != null)
            {
                this.DeleteColumnClicked(this, new EventArgs());
            }
        }

        public int ItemCount
        {
            get
            {
                return this.categoyList.Count;
            }
        }

        private Collection<string> categoyList;

        private Collection<string> columnList;

        public void AddCategoryListItem(string item)
        {
            this.categoyList.Add(item);
        }

        public void AddColumnListItem(string item)
        {
            this.columnList.Add(item);
        }

        public void RemoveAt(int index)
        {
            this.categoyList.RemoveAt(index);
        }

        public void SetViewModel(PivotTable table)
        {
            this.presenter.Model = table;
        }

        internal void Add()
        {
            OnAddNewCategoryClicked();
        }

        public int SelectedIndex { get; set; }

        public PivotTableCategory SelectedCategory
        {
            get
            {
                return this.presenter.Model.Categories[this.SelectedIndex];
            }
        }

        internal void DeleteSelectedItem()
        {
            OnDeleteCategoryClicked();
        }

        public void ClearCategoryList()
        {
            this.categoyList.Clear();
        }

        public void ClearColumnItems()
        {
            this.columnList.Clear();
        }
    }
}
